package com.itheima.pinda.impl.truck;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.pinda.DTO.entity.DGlobalUser;
import com.itheima.pinda.DTO.entity.dto.PdTruckDTO;
import com.itheima.pinda.DTO.entity.pojo.transportline.PdTransportTripsTruckDriver;
import com.itheima.pinda.DTO.entity.pojo.truck.PdTruck;
import com.itheima.pinda.common.utils.Constant;
import com.itheima.pinda.feign.service.DGlobalUserService;
import com.itheima.pinda.feign.service.transportline.PdTransportTripsTruckDriverService;
import com.itheima.pinda.feign.service.truck.PdTruckService;
import com.itheima.pinda.mapper.truck.PdTruckMapper;
import com.itheima.pinda.utils.CustomIdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 车辆信息表 服务实现类
 * </p>
 *
 * @author xy
 * @since 2021-11-12
 */
@Service
public class PdTruckServiceImpl extends ServiceImpl<PdTruckMapper, PdTruck> implements PdTruckService {

    //    中间表，通过这个中间表找到userid然后找到pdaddressbook表中的司机名，
//    并且还要通过这个表找到车次编号
    @Autowired
    private PdTransportTripsTruckDriverService transportTripsTruckDriverService;

    @Autowired
    private DGlobalUserService globalUserService;



    @Autowired
    private CustomIdGenerator idGenerator;

    @Override
    public List<PdTruckDTO> findByFleet(String fid) {
        if (ObjectUtils.isEmpty(fid)) {
            throw new NullPointerException("车队编号为空，查询失败");
        }
        QueryWrapper<PdTruck> wrapper = new QueryWrapper<>();
        wrapper.eq("fleet_id", fid);

        List<PdTruck> list = super.baseMapper.selectList(wrapper);

        List<PdTruckDTO> truckDTOS = new ArrayList<>();

        for (PdTruck truck : list) {
//            copy到dto对象中
            PdTruckDTO truckDTO = new PdTruckDTO();
            BeanUtils.copyProperties(truck, truckDTO);

//            查询出PdTransportTripsTruckDriver的车次编号和用户id
            QueryWrapper<PdTransportTripsTruckDriver> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("truck_id", truck.getId());
            PdTransportTripsTruckDriver transportTripsTruckDriver =
                    transportTripsTruckDriverService.getBaseMapper().selectOne(queryWrapper);

//            查询到的车次编号添加到dto对象中
            truckDTO.setTransportId(transportTripsTruckDriver.getTransportTripsId());

//            根据用户id查询出用户名，即司机名
            QueryWrapper<DGlobalUser> globalUserQueryWrapper = new QueryWrapper<>();
            globalUserQueryWrapper.eq("id", transportTripsTruckDriver.getUserId());
            DGlobalUser dGlobalUser = globalUserService.getBaseMapper().selectOne(globalUserQueryWrapper);

            truckDTO.setDriver(dGlobalUser.getName());


            truckDTOS.add(truckDTO);
        }

        return truckDTOS;
    }

    /**
     * 根据id查询单个对象
     * @return PdTruckDTO
     */
    @Override
    public PdTruckDTO findById(String fid, String id) {
        if (ObjectUtils.isEmpty(fid)) {
            throw new NullPointerException("车队编号为空，查询失败");
        }
        if (ObjectUtils.isEmpty(id)) {
            throw new NullPointerException("车辆类型编号为空，查询失败");
        }
        List<PdTruckDTO> truckDTOList = findByFleet(fid);

        for (PdTruckDTO dto : truckDTOList
        ) {
            if (dto.getId().equals(id)){
                return dto;
            }
        }

        return null;
    }

    @Override
    public List<PdTruckDTO> findAdvance(String fid) {
        return null;
    }


    @Override
    public List<PdTruckDTO> findAdvance(String truckId, String licensePlate, String fleetId) {

        QueryWrapper<PdTruck> wrapper = new QueryWrapper<>();

        if (!ObjectUtils.isEmpty(fleetId)){
            wrapper.eq("fleet_id", fleetId);

        }
        if (!ObjectUtils.isEmpty(licensePlate)){
            wrapper.eq("license_plate",licensePlate);
        }
        if(!ObjectUtils.isEmpty(truckId)){
            wrapper.eq("truck_id", truckId);

        }

        wrapper.eq("status", Constant.DATA_DEFAULT_STATUS);

        List<PdTruck> list = super.baseMapper.selectList(wrapper);

        List<PdTruckDTO> truckDTOS = new ArrayList<>();

        for (PdTruck truck : list) {
//            copy到dto对象中
            PdTruckDTO truckDTO = new PdTruckDTO();
            BeanUtils.copyProperties(truck, truckDTO);

//            查询出PdTransportTripsTruckDriver的车次编号和用户id
            QueryWrapper<PdTransportTripsTruckDriver> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("truck_id", truck.getId());
            PdTransportTripsTruckDriver transportTripsTruckDriver =
                    transportTripsTruckDriverService.getBaseMapper().selectOne(queryWrapper);

//            查询到的车次编号添加到dto对象中
            truckDTO.setTransportId(transportTripsTruckDriver.getTransportTripsId());

//            根据用户id查询出用户名，即司机名
            QueryWrapper<DGlobalUser> globalUserQueryWrapper = new QueryWrapper<>();
            globalUserQueryWrapper.eq("id", transportTripsTruckDriver.getUserId());
            DGlobalUser dGlobalUser = globalUserService.getBaseMapper().selectOne(globalUserQueryWrapper);

            truckDTO.setDriver(dGlobalUser.getName());



            truckDTOS.add(truckDTO);
        }

        return truckDTOS;
    }

    @Override
    public Integer insert(PdTruck truck) {
        if (ObjectUtils.isEmpty(truck)){
            throw new NullPointerException("truck对象为空");
        }
        truck.setId(idGenerator.nextId(truck)+"");
        int insert = super.baseMapper.insert(truck);
        return insert;
    }
}
